﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_DateTimePicker : System.Web.UI.UserControl
{
    public DateTime Time
    {
        get
        {
            DateTime date = new DateTime(1900,1,1);
            if (tbDate.Text != string.Empty)
                date = DateTime.Parse(tbDate.Text);
            else
                tbDate.Text = date.ToString("yyyy-MM-dd");

            int hour = 0;

            try
            {
                if (dropHour.SelectedValue != null && dropHour.SelectedValue != "00")
                    hour = int.Parse(dropHour.SelectedValue);
                else
                    dropHour.SelectedValue = hour.ToString("00");
            }
            catch
            {
            }
         

            int minute = 0;
            try
            {
                if (dropMin.SelectedValue != null && dropMin.SelectedValue != "00")
                    minute = int.Parse(dropMin.SelectedValue);
                else
                    dropMin.SelectedValue = minute.ToString("00");
            }
            catch
            { }


            return new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
        }
        set
        {
            tbDate.Text = value.ToString("yyyy-MM-dd");
            dropHour.SelectedValue = value.ToString("HH");
            //dropMin.SelectedValue = value.ToString("mm");
            int timeValue = (int.Parse(value.ToString("mm")) / 5) * 5;
            this.dropMin.SelectedValue = this.GetTimeString(timeValue);

        }
    }

    private string GetTimeString(int timeValue)
    {
        if (timeValue < 10)
        {
            return string.Format("0{0}", timeValue);
        }
        return timeValue.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime time = Time;
          
            tbDate.Attributes.Add("readonly", "true");

            string[] HourRange = new string[24];
            string[] MinRange = new string[12];
            for (int i = 0; i < 24; i++)
            {
                HourRange[i] = i.ToString("00");
            }
            int k = 0;
            for (int i = 0; i < 60; i+=5)
            {
                MinRange[k] = i.ToString("00");
                k++;
            }
            dropHour.DataSource = HourRange;
            dropHour.DataBind();

            dropMin.DataSource = MinRange;
            dropMin.DataBind();
        }

        
    }
}
